package dao.dao.impl;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.ResultSet;
import dao.dao.RowMapper;
public class ValueRowMapper<T> implements RowMapper<T> {
	private Class<?>valueClass;//
	public ValueRowMapper(Class<T> valueClass) {
		this.valueClass=valueClass;
	}
	@Override
	public T toObject(ResultSet rs) throws Exception {
		if(valueClass==int.class){
			return (T)new Integer(rs.getInt(1));
		}else if(valueClass==Integer.class){
			BigDecimal num=rs.getBigDecimal(1);
			if(num==null)return null;
			return (T)new Integer(num.intValue());
		}else if(valueClass==double.class){
			return (T) new Double(rs.getDouble(1));
		}else if(valueClass==Double.class){
			BigDecimal num=rs.getBigDecimal(1);
			if(num==null)return null;
			return (T)new Double(rs.getDouble(1));
		}else if(valueClass==float.class){
			return (T) new Float(rs.getFloat(1));
		}else if(valueClass==Float.class){
			BigDecimal num=rs.getBigDecimal(1);
			if(num==null)return null;
			return (T)new Float(rs.getFloat(1));
		}else if(valueClass==long.class){
			return (T)new Long(rs.getLong(1));
		}else if(valueClass==Long.class){
			BigDecimal num=rs.getBigDecimal(1);
			if(num==null)return null;
			return (T)new Long(rs.getLong(1));
		}else if(valueClass==String.class){
			return (T)new String(rs.getString(1));
		}else if(valueClass==Date.class){
			return (T) new Date(rs.getTimestamp(1).getTime());
		}
		return null;	
	}
}
